package com.mju.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mju.controller.common.BaseController;
import com.mju.controller.common.JsonResult;
import com.mju.pojo.Vip;
import com.mju.service.IVipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/vip")
public class VipController extends BaseController {
    @Autowired
    private IVipService vipService;

    //开通VIP
    @PostMapping("/activateVip")
    public JsonResult activateVip(@RequestBody Vip vip, @RequestParam("day")Integer day){
        String message = vipService.addNewVip(vip, day) ? "开通成功" : "开通失败";
        return new  JsonResult(OK,message);
    }

    //查询用户VIP信息
    @GetMapping("/queryVipInfo/{uid}")
    public JsonResult<Vip> queryVipInfo(@PathVariable("uid")Integer uid){
        Vip data = vipService.queryVipByUid(uid);
        return new JsonResult<>(OK,data);
    }

    //查询VIP状态
    @GetMapping("/isExpire/{uid}")
    public JsonResult isExpire(@PathVariable("uid") Integer uid){
        String message = vipService.doesTheVipExpire(uid) ? "VIP有效" : "VIP过期";
        return new JsonResult(OK,message);
    }

    //VIP续费
    @PutMapping("/renewal")
    public JsonResult renewal(
            @RequestParam("uid")Integer uid,@RequestParam("day")Integer day){
        String message = vipService.vipRenewal( uid, day) ? "续费成功" : "续费失败";
        return new JsonResult(OK,message);
    }

    //修改VIP状态
    @PutMapping("/updateVIPStatus")
    public JsonResult updateVIPStatus(@RequestParam("uid")Integer uid,@RequestParam("status")Boolean status){
        String message = vipService.updateVIPStatus(uid, status) ? "修改状态成功" : "修改状态失败";
        return new JsonResult(OK,message);
    }

    //VIP过期
    @DeleteMapping("/deleteVIP/{uid}")
    public JsonResult deleteVip(@PathVariable("uid")Integer uid){
        String message = vipService.deleteVIP(uid) ? "删除成功" : "删除失败";
        return new JsonResult(OK,message);
    }

    //无条件查询所有VIP信息
    @GetMapping("/{currentPage}/{pageSize}")
    public JsonResult<IPage<Vip>> queryAllVipInfo(
            @PathVariable("currentPage") Integer currentPage, @PathVariable("pageSize") Integer pageSize){
        IPage<Vip> data = vipService.queryAllVipInfo(currentPage, pageSize);
        return new JsonResult<>(OK,data);
    }

    //条件查询所有VIP信息
    @PostMapping("/conQueryAllVipInfo")
    public JsonResult<IPage<Vip>> conQueryAllVipInfo(
            @RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize,@RequestBody Vip vip){
        IPage<Vip> data = vipService.conQueryAllVipInfo(currentPage, pageSize,vip);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( currentPage > data.getPages()){
            data = vipService.conQueryAllVipInfo((int)data.getPages(), pageSize,vip);
        }
        return new JsonResult<>(OK,data);
    }
}
